package com.assess.web.controller;

import com.assess.web.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录临时方案，后期可升级为sso，集成多系统
 */
@Controller
@RequestMapping("login")
public class LoginController {

    public static final Map<String, String> LOGINACCOUNT;

    static {
        LOGINACCOUNT = new HashMap<String, String>();
        LOGINACCOUNT.put("dmm", "dmm");
    }

    /**
     * 跳转登录页面
     *
     * @return
     */
    @RequestMapping("view")
    public String loginView() {
        return "login";
    }

    /**
     * 登录
     *
     * @param request
     * @param loginName
     * @param pass
     * @return
     */
    @RequestMapping("do")
    public ModelAndView doLogin(
            HttpServletRequest request,
            @RequestParam("loginname") String loginName,
            @RequestParam("pwd") String pass) {

        String accountPass = LOGINACCOUNT.get(loginName);
        if (!StringUtil.isEmpty(accountPass) && accountPass.equals(pass)) {
            request.getSession().setAttribute("loginUser", loginName);
            return new ModelAndView("redirect:/main");
        }
        return new ModelAndView("redirect:/login/view");
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @RequestMapping("out")
    public ModelAndView loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUser");
        return new ModelAndView("redirect:/login/view");
    }
}
